using MediatR;
using RBAC.Domain.ProcurementManagement;
using RBAC.ErrorCode;
using RBAC.Read.Api.Application.Command.ProcurementOrderInfo;
using RBAC.Interstructrue.Base;
using Dapper;
using System;
using System.Collections.Generic;

namespace RBAC.Read.Api.Application.Handler.ProcurementOrderInfo
{
    public class GetProcurementOrderCommandHandler : IRequestHandler<GetProcurementOrderCommands, APIResult<APIPaging<ProcurementTable>>>
    {
        private readonly IBaseRepository<ProcurementTable> _repo;

        public GetProcurementOrderCommandHandler(IBaseRepository<ProcurementTable> repo)
        {
            _repo = repo;
        }

        public Task<APIResult<APIPaging<ProcurementTable>>> Handle(GetProcurementOrderCommands request, CancellationToken cancellationToken)
        {
            try
            {
                var whereParts = new List<string>();
                var param = new DynamicParameters();

                // 仅查询未删除
                whereParts.Add("IsDeleted = 0");

                if (!string.IsNullOrWhiteSpace(request.ProcurementCode))
                {
                    whereParts.Add("ProcurementCode = @ProcurementCode");
                    param.Add("@ProcurementCode", request.ProcurementCode);
                }

                if (!string.IsNullOrWhiteSpace(request.VendorName))
                {
                    whereParts.Add("VendorName LIKE @VendorName");
                    param.Add("@VendorName", $"%{request.VendorName}%");
                }

                if (!string.IsNullOrWhiteSpace(request.Keyword))
                {
                    whereParts.Add("(OrderTheme LIKE @kw OR ProcurementCode LIKE @kw OR VendorName LIKE @kw)");
                    param.Add("@kw", $"%{request.Keyword}%");
                }

                if (request.StartDate.HasValue)
                {
                    whereParts.Add("DatePurchase >= @StartDate");
                    param.Add("@StartDate", request.StartDate.Value.Date);
                }

                if (request.EndDate.HasValue)
                {
                    // 含当天，向后加一天再用 <
                    whereParts.Add("DatePurchase < @EndDateNext");
                    param.Add("@EndDateNext", request.EndDate.Value.Date.AddDays(1));
                }

                var whereClause = string.Join(" AND ", whereParts);

                var pageIndex = request.PageIndex <= 0 ? 1 : request.PageIndex;
                var pageSize = request.PageSize <= 0 ? 10 : request.PageSize;

                var (data, total) = _repo.GetPaged(pageIndex, pageSize, whereClause, "CreateTime DESC", param);

                return Task.FromResult(new APIResult<APIPaging<ProcurementTable>>
                {
                    Code = APIEnums.Success,
                    Msg = "查询成功",
                    Data = new APIPaging<ProcurementTable>
                    {
                        TotalCount = total,
                        PageDatas = data
                    }
                });
            }
            catch (Exception ex)
            {
                return Task.FromResult(new APIResult<APIPaging<ProcurementTable>>
                {
                    Code = APIEnums.Fail,
                    Msg = "查询失败：" + ex.Message,
                    Data = new APIPaging<ProcurementTable>
                    {
                        TotalCount = 0,
                        PageDatas = new List<ProcurementTable>()
                    }
                });
            }
        }
    }
}
